package com.serviceImpl;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.dao.manager.InternalMapper;
import com.pojo.Internal;
import com.service.InternalService;
import com.utils.QRCodeUtil;
import com.utils.UUIDUtils;
import com.utils.UploadUtils;

@Service
public class InternalServiceImp implements InternalService {

	@Resource
	private InternalMapper internalMapper;
	@Value("${img.upload.path}")
	private String imgUploadPath;
	@Value("${img.access.path}")
	private String imgAccessPath;

	@Override
	public int addInternal(Map<String, String> paramsMap) {
		try {
			String uuid = UUIDUtils.getUUID();
			// 生成二维码
			String path = paramsMap.get("pathString") + File.separator + uuid + ".JPG";
			QRCodeUtil.encode(paramsMap.get("contextPath") + "/internal/getInternal?uuid=" + uuid, "", path);
			// 把file转换为MultipartFile, 并调用七牛进行上传
			File ewmFile = new File(path);
			FileInputStream input = new FileInputStream(ewmFile);
			MultipartFile multipartFile = new MockMultipartFile("file", uuid + ".JPG", "text/plain",
					IOUtils.toByteArray(input));
			MultipartFile[] ewmfiles = new MultipartFile[] { multipartFile };
			List<String> ewmPaths = UploadUtils.uploadQiniuImg(ewmfiles, imgUploadPath, imgAccessPath);
			Internal entity = new Internal();
			if (ewmPaths != null && !ewmPaths.isEmpty()) {
				entity.setEwmimg(ewmPaths.get(0));
			}
			entity.setUuid(uuid);
			entity.setName(paramsMap.get("name"));
			internalMapper.insert(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<Map<String, String>> getInternal(Map<String, String> paramsMap) {
		return internalMapper.getInternal(paramsMap.get("uuid"));
	}

	@Override
	public List<Map<String, String>> getInternalByName(Map<String, String> paramsMap) {
		String page = paramsMap.get("page");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		String num = paramsMap.get("num");
		if (num == null || "".equals(num)) {
			num = "10";
			paramsMap.put("num", num);
		}
		int start = (Integer.parseInt(page) - 1) * Integer.parseInt(num);
		paramsMap.put("start", start + "");
		return internalMapper.getInternalByName(paramsMap);
	}

	@Override
	public int updateInternal(Map<String, String> paramsMap) {
		try {
			String uuid = paramsMap.get("uuid");
			// 生成二维码
			String path = paramsMap.get("pathString") + File.separator + uuid + ".JPG";
			QRCodeUtil.encode(paramsMap.get("contextPath") + "/internal/getInternal?uuid=" + uuid, "", path);
			// 把file转换为MultipartFile, 并调用七牛进行上传
			File ewmFile = new File(path);
			FileInputStream input = new FileInputStream(ewmFile);
			MultipartFile multipartFile = new MockMultipartFile("file", uuid + ".JPG", "text/plain",
					IOUtils.toByteArray(input));
			MultipartFile[] ewmfiles = new MultipartFile[] { multipartFile };
			List<String> ewmPaths = UploadUtils.uploadQiniuImg(ewmfiles, imgUploadPath, imgAccessPath);
			Internal entity = new Internal();
			if (ewmPaths != null && !ewmPaths.isEmpty()) {
				entity.setEwmimg(ewmPaths.get(0));
			}
			entity.setUuid(uuid);
			entity.setName(paramsMap.get("name"));
			internalMapper.updateByPrimaryKeySelective(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int deleteInternal(Map<String, String> paramsMap) {
		try {
			return internalMapper.deleteInternal(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	@Override
	public List<Map<String, String>> getAllInterInfoByName(Map<String, String> paramsMap) {
		return internalMapper.getAllInterInfoByName(paramsMap);
	}

	@Override
	public Map<String, String> getInternalByUuid(Map<String, String> paramsMap) {
		// TODO Auto-generated method stub
		return internalMapper.getInternalByUuid(paramsMap.get("uuid"));
	}

	@Override
	public List<Map<String, String>> getQrcode(Map<String, String> paramsMap) {
		return internalMapper.getQrcode(paramsMap);
	}

}
